/**
 * Project Name:mauto-web
 * File Name:AreaService.java
 * Package Name:com.inftech.mauto.sys.service
 * Date:2016年10月7日下午9:14:13
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.sys.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.inftech.mauto.common.constant.BusinessConstant;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.sys.dao.ICarBasicDao;
import com.inftech.mauto.sys.entity.CarBrand;
import com.inftech.mauto.sys.entity.CarModel;
import com.mingsoft.base.biz.impl.BaseBizImpl;
import com.mingsoft.base.dao.IBaseDao;

/**
 * ClassName:CarBasicService <br/>
 * Function: 车型选择service. <br/>
 * Date: 2016年10月7日 下午9:14:13 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service("carBasicService")
public class CarBasicService extends BaseBizImpl {

	/**
	 * 车型选择持久化层注入
	 */
	@Autowired
	private ICarBasicDao carBasicDao;

	/**
	 * 后台服务器地址
	 */
	@Value("${adminUrl}")
	private String adminUrl;

	@Override
	protected IBaseDao getDao() {
		return carBasicDao;
	}

	/**
	 * 
	 * queryCarBrandList:查询车辆品牌列表. <br/>
	 *
	 * @author puyf
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String, List<Map<String, Object>>> queryCarBrandList() throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("objectType", BusinessConstant.FILE_TYPE_CAR_BRAND_IMG);
		// 查询数据库中车辆品牌列表
		List<CarBrand> carBrandList = carBasicDao.queryCarBrandList(paramMap);
		// 返回map
		Map<String, List<Map<String, Object>>> resultMap = new HashMap<String, List<Map<String, Object>>>();
		if (carBrandList != null && carBrandList.size() != 0) {
			for (CarBrand carBrand : carBrandList) {
				// 品牌首字母
				String cbInitial = carBrand.getCbInitial();
				Map<String, Object> carBrandM = new HashMap<String, Object>();
				// 品牌id
				carBrandM.put("id", carBrand.getId());
				// 品牌名称
				carBrandM.put("cbName", carBrand.getCbName());
				// 品牌图片
				if (!CommonUtils.isBlank(carBrand.getCbImg())) {
					carBrandM.put("cbImg", adminUrl + carBrand.getCbImg());
				}
				// 获取首字母对应的列表
				List<Map<String, Object>> bMapList = resultMap.get(cbInitial);
				if (bMapList == null) {
					bMapList = new ArrayList<Map<String, Object>>();
				}
				bMapList.add(carBrandM);
				if (bMapList.size() == 1) {
					resultMap.put(cbInitial, bMapList);
				}
				if (carBrand.getCbIsHot()) {
					// 为热门品牌
					List<Map<String, Object>> bMapListHot = resultMap.get(BusinessConstant.HOT_CAR_BRAND);
					if (bMapListHot == null) {
						bMapListHot = new ArrayList<Map<String, Object>>();
					}
					bMapListHot.add(carBrandM);
					if (bMapListHot.size() == 1) {
						resultMap.put(BusinessConstant.HOT_CAR_BRAND, bMapListHot);
					}
				}
			}
		}
		// 二次转换
		Map<String, List<Map<String, Object>>> resultMap2 = new HashMap<String, List<Map<String, Object>>>();
		resultMap2.put(BusinessConstant.HOT_CAR_BRAND, resultMap.get(BusinessConstant.HOT_CAR_BRAND));

		List<Map<String, Object>> brandsList = new ArrayList<Map<String, Object>>();
		for (Entry<String, List<Map<String, Object>>> entry : resultMap.entrySet()) {
			if (!entry.getKey().equals(BusinessConstant.HOT_CAR_BRAND)) {
				Map<String, Object> brandMap = new HashMap<String, Object>();
				brandMap.put("key", entry.getKey());
				brandMap.put("list", entry.getValue());
				brandsList.add(brandMap);
			}
		}
		resultMap2.put(BusinessConstant.CAR_BRAND, brandsList);
		return resultMap2;
	}

	/**
	 * 
	 * queryCarModelList:查询车辆型号列表. <br/>
	 *
	 * @author puyf
	 * @param cbId
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String, Object> queryCarModelList(int cbId) throws Exception {
		// 查询数据库中车型列表
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", cbId);
		paramMap.put("objectType", BusinessConstant.FILE_TYPE_CAR_BRAND_IMG);
		CarBrand carBrand = carBasicDao.queryCarBrand(paramMap);
		List<CarModel> carModelList = carBasicDao.queryCarModelList(cbId);
		// 车型map
		Map<String, Map<String, Set>> modelMap = new HashMap<String, Map<String, Set>>();
		if (carModelList != null && carModelList.size() != 0) {
			for (CarModel carModel : carModelList) {
				// 厂商
				String cmManufacturer = carModel.getCmManufacturer();
				Map<String, Set> mSet = modelMap.get(cmManufacturer);
				// 品牌类型
				String cmName = carModel.getCmName();
				// 排量和年份
				Set nSet = null;
				if (mSet == null) {
					mSet = new HashMap<String, Set>();
					modelMap.put(cmManufacturer, mSet);
					nSet = new LinkedHashSet();
				} else {
					nSet = mSet.get(cmName);
					if (nSet == null) {
						nSet = new LinkedHashSet();
					}
				}
				nSet.add(carModel.getCmDisplacement() + " " + carModel.getCmProductiveYear());
				if (nSet.size() == 1) {
					mSet.put(cmName, nSet);
				}

				// if(mSet1 == null){
				// mSet1 = new LinkedHashSet<>();
				// }
				// mSet.add(carModel.getCmName());
				// if (mSet.size() == 1) {
				// modelMap.put(cmManufacturer, mSet);
				// }
			}
		}
		// 返回map
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("cbName", carBrand.getCbName());
		if (!CommonUtils.isBlank(carBrand.getCbImg())) {
			resultMap.put("cbImg", adminUrl + carBrand.getCbImg());
		}
		resultMap.put("models", modelMap);
		return resultMap;
	}

	/**
	 * 
	 * getCarDetail:(获取车辆信息). <br/>
	 *
	 * @author Administrator
	 * @param peopleId
	 * @return
	 * @throws SQLException
	 * @since JDK 1.7
	 */
	public Map<String, Object> getCarDetail(Integer peopleId) throws SQLException {
		return carBasicDao.getCarDetail(peopleId);
	}
	
	/**
	 * 
	 * getCnName:(获取车辆类型). <br/>
	 *
	 * @author Administrator
	 * @return
	 * @since JDK 1.7
	 */
	public List<CarModel> getCnName(){
		return carBasicDao.getCnName();
	}
}
